#pragma once

class ModifyEmailHandler : public JsonRequest<ModifyEmailHandler> 
{
public:
	bool response()
	{
		std::string		email;
		std::string		smtp;
		std::string		smtp_port;
		std::string		pop;
		std::string		pop_port;
		std::string		user;
		std::string		pwd;
		sql_string<>	sql;

		if( false == request::post("email", email)
				|| false == request::post("smtp", smtp)
				|| false == request::post("smtp_port", smtp_port)
				|| false == request::post("pop", pop)
				|| false == request::post("pop_port", pop_port)
				|| false == request::post("user", user)
				|| false == request::post("pwd", pwd) )
		{
			err << "invalid paramter";
			return false;
		}
		
		sql.format("update t_emails set smtp_ip='%q', smtp_port=%d, "
				" pop_ip='%q', pop_port=%d, user='%q', pwd='%q'"
				" where addr='%q'",
				smtp.c_str(),
				atoi(smtp_port.c_str()),
				pop.c_str(),
				atoi(pop_port.c_str()),
				user.c_str(),
				pwd.c_str(),
				email.c_str());
		if(1 != sql::execute(sql.c_str()))
		{
			err << "modify email failed:"
				<< mysql_error(g_mysql);
			return false;
		}
		
		json.push_back(JSONNode("errno", 0));
		return true;
	}
};
